var _function = require("../../utils/util.js");
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({
  data: {
    flag: true,
    taget: true,
    userCellId: 0, 
    apiurl: app.data.apiurl,
     price:'',
     snum:'',
     spec:'',
     gid:'',
     gfare:'',
     goodshow:[],
     goodsdetails:[],
     goodsthumb:'',
     openid: app.globalData.openid,
    // input默认是1 
    num: 1,
    // 使用data数据对象设置样式名 
    minusStatus: 'disabled',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    userInfo: {},
    withdata:''
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      gid: options.gid,
      gfare:options.gfare
    });
    wx.setNavigationBarTitle({
      title: '商品详情'
    });
    //商品详情
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/getgoods',
      data: {gid:options.gid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var goodshow = _function.trimEnd(res.data.data[0]['goodshow'],'|');
        var goodstext= res.data.data[0]['goodstext'];
        WxParse.wxParse('goodstext', 'html', goodstext, that, 5);
        console.log();
        that.setData({
          goods: res.data,
          goodshow: goodshow.split("|"),
          goodsthumb: res.data.data[0]['goodsthumb'],
          goodsdetails: res.data.data[0]['goodsdetails'],
        });
      }
    });
    //商品规格
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/specs',
      data: {gid:options.gid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log("111" + res.data.data[0].sprice);
        // console.log(res.data);
        that.setData({
          specs: res.data, 
          price: res.data.data[0].sprice,
          snum: res.data.data[0].snum,
          spec: res.data.data[0].sspec,
        });
      }
    });
    
  },
  
  formSubmit: function (e) {
    var that=this
    // console.log('form发生change事件，携带value值为：', e.detail.value);
    //购买数量
    console.log(e.detail.value.onum);
    //商品规格
    console.log(this.data.spec);
    //商品单价
    console.log(this.data.price);
    //商品id
    console.log(this.data.gid);
    //运费
    console.log(e.detail.value.gfare);
    // that.setData({
    //   gfare: e.detail.value.gfare,
    // })
    console.log('0000' + this.data.gfare);
    //添加购物车
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/addshoporder?gid=' + this.data.gid + '&spec=' + this.data.spec + '&price=' + this.data.price + '&onum=' + e.detail.value.onum + '&gtitle=' + e.detail.value.gtitle + '&openid='+app.globalData.openid,
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })
      }
    });
  },
  a: function () {
    this.setData({ flag: false })
  }, 
  b: function () {
    this.setData({ flag: true })
  },

  c: function () {
    this.setData({ taget: false })
  },
  d: function () {
    this.setData({ taget: true })
  },
  
  swichNav: function (e) {
    this.setData({
      userCellId: e.target.dataset.idx,
      price: e.target.dataset.price,
      snum: e.target.dataset.snum,
      spec: e.target.dataset.spec,
    })
  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减 
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1 
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回 
    this.setData({
      num: num
    });
  } ,
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.goods.data[0]['gtitle'],
      path: '/pages/detail/detail?gid='+this.data.gid,
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '转发失败',
          icon: 'clear',
          duration: 2000,
          image: '../../images/tanhao.png'
        });
      }
    }
  } 
})